package org.dromara.merchants.service.impl;

import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.merchants.domain.TaGoods;
import org.dromara.merchants.domain.TaSetMeal;
import org.dromara.merchants.domain.vo.TaGoodsVo;
import org.dromara.merchants.domain.vo.TaSetMealVo;
import org.dromara.merchants.mapper.TaGoodsMapper;
import org.dromara.merchants.mapper.TaSetMealMapper;
import org.springframework.stereotype.Service;
import org.dromara.merchants.domain.bo.TaCategoryBo;
import org.dromara.merchants.domain.vo.TaCategoryVo;
import org.dromara.merchants.domain.TaCategory;
import org.dromara.merchants.mapper.TaCategoryMapper;
import org.dromara.merchants.service.ITaCategoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 公共分类Service业务层处理
 *
 * @author xiyin321
 * @date 2024-07-02
 */
@RequiredArgsConstructor
@Service
public class TaCategoryServiceImpl implements ITaCategoryService {

    private final TaCategoryMapper baseMapper;
    private final TaGoodsMapper goodsMapper;
    private final TaSetMealMapper setMealMapper;
    /**
     * 查询公共分类
     */
    @Override
    public TaCategoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询公共分类列表
     */
    @Override
    public TableDataInfo<TaCategoryVo> queryPageList(TaCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaCategory> lqw = buildQueryWrapper(bo);
        Page<TaCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询公共分类列表
     */
    @Override
    public List<TaCategoryVo> queryList(TaCategoryBo bo) {
        LambdaQueryWrapper<TaCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TaCategory> buildQueryWrapper(TaCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), TaCategory::getCategoryName, bo.getCategoryName());
        return lqw;
    }

    /**
     * 新增公共分类
     */
    @Override
    public Boolean insertByBo(TaCategoryBo bo) {
        TaCategory add = MapstructUtils.convert(bo, TaCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCategoryId(add.getCategoryId());
        }
        return flag;
    }

    /**
     * 修改公共分类
     */
    @Override
    public Boolean updateByBo(TaCategoryBo bo) {
        TaCategory update = MapstructUtils.convert(bo, TaCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TaCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除公共分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            /*查询在goods或者setMeal中是否有公共分类正在使用*/
            List<TaGoodsVo> goodsList = goodsMapper
                .selectVoList(Wrappers.<TaGoods>lambdaQuery().in(TaGoods::getCategoryId, ids));
            if (!goodsList.isEmpty()) {
                throw new ServiceException("该公共分类正在被商品使用，无法删除");
            }
            List<TaSetMealVo> setMealList = setMealMapper
                .selectVoList(Wrappers.<TaSetMeal>lambdaQuery().in(TaSetMeal::getCategoryId, ids));
            if (!setMealList.isEmpty()) {
                throw new ServiceException("该公共分类正在被套餐使用，无法删除");
            }
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
